﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HHHFramework {
    public enum ResState
    {
        Waiting,//刚刚创建好res对象还没有进行加载
        Loading,//资源正在加载
        Loaded,//资源加载成功
    }


    /// <summary>
    /// 根据不同的资源进行资源加载和卸载
    /// </summary>
    public abstract class Res : SimpleRC,IRes
    {
        /// <summary>
        ///加载状态
        /// </summary>
        protected ResState mRestate;
     
        protected string mAssetName;
        /// <summary>
        /// 资源名称   resources  全称  assetbundle中asset asset
        /// </summary>
        public string AssetName {
            get { return this.mAssetName; }
            set { mAssetName = value; }
        }
        /// <summary>
        /// assetbundle的名称
        /// </summary>
        public string OwnerBundleName {
            get;
            set;
        }
        /// <summary>
        /// 需要加载的资源类型
        /// </summary>
        public Type AssetType
        {
            get;
            set;
        }
        public ResState State{
            get { return mRestate; }
            set {
                mRestate = value;
                if (mRestate==ResState.Loaded) {
                    //Debug.Log(this.mAssetName + "完成加载");
                    mOnLoadedEvent?.Invoke(this);
                }
                
            } }
      
        public UnityEngine.Object Asset { get; protected set; }
        public bool Equals(ResSearchKeys resSearchKeys)
        {
            if (resSearchKeys.AssetName == mAssetName && resSearchKeys.OwnerBundle == OwnerBundleName && resSearchKeys.AssetType == AssetType)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Load 同步
        /// </summary>
        public abstract bool LoadSync();

        public abstract void LoadAsync();
        protected abstract void OnReleaseRes();

        protected override void OnZeroRef()
        {
            OnReleaseRes();
        }
        private event Action<Res> mOnLoadedEvent;

        public void RegisterOnLoadedEvent(Action<Res> onLoaded) {
            mOnLoadedEvent += onLoaded;
        }
        public void UnRegisterOnLoadedEvent(Action<Res> onLoaded) {
            mOnLoadedEvent = onLoaded;
        }
    }
}

